home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 376-400 / disk_386 / xlispstat / src2.lzh / XLisp-Stat / xsiview3.c < prev    next >
C/C++ Source or Header  |  1990-10-08  |  14KB  |  509 lines

  1. /* xsiview3 - XLISP interface to IVIEW dynamic graphics package.       */
  2. /* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
  3. /*Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
  4. /* You may give out copies of this software; for conditions see the    */
  5. /* file COPYING included with this distribution.                       */
  6.  
  7. #include <string.h>
  8. #include "xlisp.h"
  9. #include "osdef.h"
  10. #ifdef ANSI
  11. #include "xlproto.h"
  12. #include "xlsproto.h"
  13. #include "iviewproto.h"
  14. #include "Stproto.h"
  15. #else
  16. #include "xlfun.h"
  17. #include "xlsfun.h"
  18. #include "iviewfun.h"
  19. #include "Stfun.h"
  20. #endif ANSI
  21. #include "xlsvar.h"
  22.  
  23. /* forward declarations */
  24. #ifdef ANSI
  25. LVAL base_variable_label(void),variable_label(void),base_range(void),
  26.   range(void),base_screen_range(void),screen_range(void),
  27.   newmatrix(unsigned,unsigned),make_transformation(double **,int),
  28.   draw_data(int);
  29. void set_internal_transformation(int,LVAL,LVAL);
  30. #else
  31. LVAL base_variable_label(),variable_label(),base_range(),
  32.   range(),base_screen_range(),screen_range(),
  33.   newmatrix(),make_transformation(),draw_data();
  34. void set_internal_transformation();
  35. #endif ANSI
  36.  
  37. /* static global variables */
  38. static int maxvars = 0;
  39. static double **transform, *transformdata;
  40. static int *inbasis;
  41. static IVIEW_WINDOW wind;
  42. static int range_type;
  43.  
  44. /**************************************************************************/
  45. /**                                                                      **/
  46. /**                    General IView Data Functions                      **/
  47. /**                                                                      **/
  48. /**************************************************************************/
  49.  
  50. static LVAL base_variable_label()
  51. {
  52.   int var, set = FALSE;
  53.   char *label;
  54.   LVAL result;
  55.   
  56.   var = getfixnum(xlgafixnum());
  57.   if (moreargs()) {
  58.     set = TRUE;
  59.     label = (char *) getstring(xlgastring());
  60.   }
  61.   xllastarg();
  62.   
  63.   if (set) IViewSetVariableLabel(wind, var, label);
  64.   
  65.   label = IViewVariableLabel(wind, var);
  66.   if (label == nil) result = newstring(1);
  67.   else {
  68.     result = newstring(strlen(label) + 1);
  69.     strcpy(getstring(result), label);
  70.   }
  71.   
  72.   return(result);
  73. }
  74.  
  75. static LVAL variable_label()
  76. {
  77.   return(recursive_subr_map_elements(base_variable_label, variable_label));
  78.  
  79. LVAL iview_variable_label()
  80. {
  81.   wind = get_iview_address(xlgaobject());
  82.   return(variable_label());
  83. }
  84.  
  85. static LVAL base_range()
  86. {
  87.   int var, set = FALSE;
  88.   double low, high;
  89.   
  90.   var = getfixnum(xlgafixnum());
  91.   if (moreargs()) {
  92.     set = TRUE;
  93.     low = makedouble(xlgetarg());
  94.     high = makedouble(xlgetarg());
  95.   }
  96.   
  97.   if (set) {
  98.     if (range_type != 'S') IViewSetRange(wind, var, low, high);
  99.     else IViewSetScaledRange(wind, var, low, high);
  100.   }
  101.   if (range_type != 'S') IViewGetRange(wind, var, &low, &high);
  102.   else IViewGetScaledRange(wind, var, &low, &high);
  103.  
  104.   return(double_list_2(low, high));
  105. }
  106.  
  107. static LVAL range()
  108. {
  109.   return(recursive_subr_map_elements(base_range, range));
  110. }
  111.  
  112. LVAL iview_range()
  113. {
  114.   LVAL object = xlgaobject(), result;            /* added JKL */
  115.   int set = (xlargc  > 1) ? TRUE : FALSE, draw, showing = NIL, a, b;
  116.  
  117.   wind = get_iview_address(object);
  118.   StGrGetContentVariables(IViewWindowWinInfo(wind),&a,&b);/* 3 lines added JKL */
  119.   if(getfixnum(xlargv[0])==a)IViewGetXaxis(wind, &showing, 0, 0);
  120.   else if(getfixnum(xlargv[0])==b)IViewGetYaxis(wind, &showing, 0, 0);
  121.   draw = draw_key_arg(TRUE) && showing;  /* && added JKL */
  122.   range_type = 'N';
  123.   result = range();
  124.   if (set) check_redraw(object, draw, FALSE);
  125.   return(result);
  126. }
  127.  
  128. LVAL iview_scaled_range()
  129. {
  130.   LVAL object = xlgaobject(), result;            /* added JKL */
  131.   int set = (xlargc  > 1) ? TRUE : FALSE, draw, showing = NIL, a, b;
  132.  
  133.   wind = get_iview_address(object);
  134.   StGrGetContentVariables(IViewWindowWinInfo(wind),&a,&b);/* 3 lines added JKL */
  135.   if(getfixnum(xlargv[0])==a)IViewGetXaxis(wind, &showing, 0, 0);
  136.   else if(getfixnum(xlargv[0])==b)IViewGetYaxis(wind, &showing, 0, 0);
  137.   draw = draw_key_arg(TRUE) && showing;  /* && added JKL */
  138.   range_type = 'S';
  139.   result = range();
  140.   if (set) check_redraw(object, draw, FALSE);
  141.   return(result);
  142. }
  143.  
  144. static LVAL base_screen_range()
  145. {
  146.   int var, set = FALSE;
  147.   int low, high;
  148.   
  149.   var = getfixnum(xlgafixnum());
  150.   if (moreargs()) {
  151.     set = TRUE;
  152.     low = getfixnum(xlgafixnum());
  153.     high = getfixnum(xlgafixnum());
  154.   }
  155.   xllastarg();
  156.   
  157.   if (set) IViewSetScreenRange(wind, var, low, high);
  158.   IViewGetScreenRange(wind, var, &low, &high);
  159.   
  160.   return(integer_list_2(low, high));
  161. }
  162.  
  163. static LVAL screen_range()
  164. {
  165.   return(recursive_subr_map_elements(base_screen_range, screen_range));
  166. }
  167.  
  168. LVAL iview_screen_range()
  169. {
  170.   wind = get_iview_address(xlgaobject());
  171.   return(screen_range());
  172. }
  173.  
  174. static void set_internal_transformation(vars, m, b)
  175.     int vars;
  176.     LVAL m, b;
  177. {
  178.   int i, j, k, rows, cols;
  179.   LVAL data;
  180.   
  181.   if (vars <= 0) return;
  182.   if (vars > maxvars) {
  183.     maxvars = 0;
  184.     StFree(transformdata);
  185.     StFree(transform);
  186.     StFree(inbasis);
  187.     transformdata = (double *) StCalloc(vars * vars, sizeof(double));
  188.     transform = (double **) StCalloc(vars, sizeof(double *));
  189.     for (i = 0; i < vars; i++) transform[i] = transformdata + vars * i;
  190.     inbasis = (int *) StCalloc(vars, sizeof(double));
  191.     maxvars = vars;
  192.   }
  193.   
  194.   if (! matrixp(m)) xlerror("not a matrix", m);
  195.   rows = numrows(m);
  196.   cols = numcols(m);
  197.   if (rows > vars) rows = vars;
  198.   if (cols > vars) cols = vars;
  199.   if (rows != cols) xlerror("bad transformation matrix", m);
  200.  
  201.   /* fill in upper left corner of transform from m; rest is identity */
  202.   data = arraydata(m);
  203.   for (i = 0, k = 0; i < rows; i++) {
  204.     for (j = 0; j < cols; j++, k++)
  205.       transform[i][j] = makedouble(getelement(data, k));
  206.     for (j = cols; j < vars; j++)
  207.       transform[i][j] = (i == j) ? 1.0 : 0.0;
  208.   }
  209.   for (i = rows; i < vars; i++)
  210.     for (j = 0; j < vars; j++)
  211.       transform[i][j] = (i == j) ? 1.0 : 0.0;
  212.     
  213.   /* figure out basis elements using b and size of m */
  214.   if (b != NIL) {
  215.     if (! sequencep(b)) xlerror("not a sequence", b);
  216.     if (seqlen(b) != rows) xlerror("wrong length for basis", b);
  217.     for (i = 0; i < rows; i++)
  218.       inbasis[i] = (getnextelement(&b, i) != NIL) ? TRUE : FALSE;
  219.   }
  220.   else for (i = 0; i < rows; i++) inbasis[i] = TRUE;
  221.   for (i = rows; i < vars; i++) inbasis[i] = FALSE;
  222. }
  223.  
  224. static LVAL newmatrix(r, c)
  225.     unsigned r, c;
  226. {
  227.   LVAL rows, cols, dim, result;
  228.   
  229.   
  230.   xlstkcheck(3);
  231.   xlsave(rows);
  232.   xlsave(cols);
  233.   xlsave(dim);
  234.   
  235.   rows = cvfixnum((FIXTYPE) r);
  236.   cols = cvfixnum((FIXTYPE) c);
  237.   dim = list2(rows, cols);
  238.   result = newarray(dim, NIL, NIL);
  239.   xlpopn(3);
  240.   
  241.   return(result);
  242. }
  243.  
  244. static LVAL make_transformation(a, vars)
  245.     double **a;
  246.     int vars;
  247. {
  248.   LVAL result, data;
  249.   int i, j, k;
  250.   
  251.   if (a == nil) return(NIL);
  252.   
  253.   xlsave1(result);
  254.   result = newmatrix(vars, vars);
  255.   data = arraydata(result);
  256.   for (i = 0, k = 0; i < vars; i++)
  257.     for (j = 0; j < vars; j++, k++)
  258.       setelement(data, k, cvflonum((FLOTYPE) a[i][j]));
  259.   xlpop();
  260.   return(result);
  261. }
  262.  
  263. LVAL iview_transformation()
  264. {
  265.   IVIEW_WINDOW w;
  266.   LVAL m, object;
  267.   int set = FALSE;
  268.   int vars;
  269.   
  270.   object = xlgaobject();
  271.   w = get_iview_address(object);
  272.   if (moreargs()) {
  273.     set = TRUE;
  274.     m = xlgetarg();
  275.   }
  276.   
  277.   vars = IViewNumVariables(w);
  278.   if (set) {
  279.     if (m == NIL) IViewSetIdentityTransformation(w);
  280.     else {
  281.       set_internal_transformation(vars, m, NIL);
  282.       IViewSetTransformation(w, transform);
  283.     }
  284.     check_redraw(object, TRUE, TRUE);
  285.   }
  286.   else m = (IViewIsTransformed(w))
  287.          ? make_transformation(IViewTransformation(w), vars) : NIL;
  288.   
  289.   return(m);
  290. }
  291.  
  292. LVAL iview_apply_transformation()
  293. {
  294.   IVIEW_WINDOW w;
  295.   LVAL m, b, object;
  296.   int vars;
  297.  
  298.   object = xlgaobject();
  299.   w = get_iview_address(object);
  300.   m = xsgetmatrix();
  301.   if (! xlgetkeyarg(sk_basis, &b)) b = NIL;
  302.  
  303.   vars = IViewNumVariables(w);
  304.   set_internal_transformation(vars, m, b);
  305.   IViewApplyTransformation(w, transform, inbasis);
  306.   check_redraw(object, TRUE, TRUE);
  307.   
  308.   return(NIL);
  309. }
  310.  
  311. /**************************************************************************/
  312. /**                                                                      **/
  313. /**                     IView Data Drawing Functions                     **/
  314. /**                                                                      **/
  315. /**************************************************************************/
  316.  
  317. static LVAL draw_data(which)
  318.     int which;
  319. {
  320.   IVIEW_WINDOW w;
  321.   int var1, var2, m, n;
  322.   
  323.   w = get_iview_address(xlgaobject());
  324.   var1 = getfixnum(xlgafixnum());
  325.   var2 = getfixnum(xlgafixnum());
  326.   m = getfixnum(xlgafixnum());
  327.   n = getfixnum(xlgafixnum());
  328.   xllastarg();
  329.   
  330.   switch(which) {
  331.   case 'P': IViewDrawDataPoints(w, var1, var2, m, n); break;
  332.   case 'L': IViewDrawDataLines(w, var1, var2, m, n); break;
  333. #ifdef USESTRINGS
  334.   case 'S': IViewDrawDataStrings(w, var1, var2, m, n); break;
  335. #endif /* USESTRINGS */
  336.   }
  337.   return(NIL);
  338. }
  339.  
  340. LVAL iview_draw_data_points()  { return(draw_data('P')); }
  341. LVAL iview_draw_data_lines()   { return(draw_data('L')); }
  342. #ifdef USESTRINGS
  343. LVAL iview_draw_data_strings() { return(draw_data('S')); }
  344. #endif /* USESTRINGS */
  345.  
  346. /**************************************************************************/
  347. /**                                                                      **/
  348. /**                     Standard Callback Functions                      **/
  349. /**                                                                      **/
  350. /**************************************************************************/
  351.  
  352. LVAL iview_std_mark_points_in_rect()
  353. {
  354.   IVIEW_WINDOW w;
  355.   int left, top, width, height;
  356.  
  357.   w = get_iview_address(xlgaobject());
  358.   left = getfixnum(xlgafixnum());
  359.   top = getfixnum(xlgafixnum());
  360.   width = getfixnum(xlgafixnum());
  361.   height = getfixnum(xlgafixnum());
  362.   xllastarg();
  363.   
  364.   IViewStdMarkPointsInRect(w, left, top, width, height);
  365.   return(NIL);
  366. }
  367.  
  368. LVAL iview_std_adjust_screen()
  369. {
  370.   IVIEW_WINDOW w;
  371.   
  372.   w = get_iview_address(xlgaobject());
  373.   IViewStdAdjustScreen(w);
  374.   return(NIL);
  375. }
  376.  
  377. PointState decode_point_state(state)
  378.     LVAL state;
  379. {
  380.   if (state == s_invisible) return(pointInvisible);
  381.   else if (state == s_normal) return(pointNormal);
  382.   else if (state == s_hilited) return(pointHilited);
  383.   else if (state == s_selected) return(pointSelected);
  384.   else xlerror("unknown point state", state);
  385. }
  386.   
  387. LVAL iview_std_adjust_points_in_rect()
  388. {
  389.   IVIEW_WINDOW w;
  390.   int left, top, width, height;
  391.   PointState state;
  392.   
  393.   w = get_iview_address(xlgaobject());
  394.   left = getfixnum(xlgafixnum());
  395.   top = getfixnum(xlgafixnum());
  396.   width = getfixnum(xlgafixnum());
  397.   height = getfixnum(xlgafixnum());
  398.   state = decode_point_state(xlgetarg());
  399.   xllastarg();
  400.   
  401.   IViewStdAdjustPointsInRect(w, left, top, width, height, state);
  402.   return(NIL);
  403. }
  404.  
  405. LVAL iview_std_adjust_screen_point()
  406. {
  407.   LVAL object;
  408.   int point;
  409.  
  410.   object = xlgaobject();
  411.   point = getfixnum(xlgafixnum());
  412.   xllastarg();
  413.  
  414.   IViewStdAdjustScreenPoint(get_iview_address(object), point);
  415.   return(NIL);
  416. }
  417.  
  418. /**************************************************************************/
  419. /**                                                                      **/
  420. /**                       IView Rotation Functions                       **/
  421. /**                                                                      **/
  422. /**************************************************************************/
  423.  
  424. LVAL iview_rotate_2()
  425. {
  426.   IVIEW_WINDOW w;
  427.   int var1, var2;
  428.   double alpha;
  429.   LVAL object;
  430.   
  431.   object = xlgaobject();
  432.   w = get_iview_address(object);
  433.   var1 = getfixnum(xlgafixnum());
  434.   var2 = getfixnum(xlgafixnum());
  435.   alpha = makedouble(xlgetarg());
  436.   
  437.   IViewRotate2(w, var1, var2, alpha);
  438.   check_redraw(object, TRUE, TRUE);
  439.   
  440.   return(NIL);
  441. }
  442.  
  443. /**************************************************************************/
  444. /**                                                                      **/
  445. /**                        Miscellaneous Functions                       **/
  446. /**                                                                      **/
  447. /**************************************************************************/
  448.  
  449. LVAL iview_get_nice_range()
  450. {
  451.   double low, high;
  452.   int ticks;
  453.   LVAL temp, result;
  454.   
  455.   low = makedouble(xlgetarg());
  456.   high = makedouble(xlgetarg());
  457.   ticks = getfixnum(xlgafixnum());
  458.   xllastarg();
  459.   
  460.   GetNiceRange(&low, &high, &ticks);
  461.   xlstkcheck(2);
  462.   xlsave(result);
  463.   xlsave(temp);
  464.   temp = cvfixnum((FIXTYPE) ticks); result = consa(temp);
  465.   temp = cvflonum((FLOTYPE) high); result = cons(temp, result);
  466.   temp = cvflonum((FLOTYPE) low); result = cons(temp, result);  
  467.   xlpopn(2);
  468.   
  469.   return(result);
  470. }
  471.  
  472. LVAL iview_adjust_depth_cuing()
  473. {
  474.   LVAL object;
  475.   int vz;
  476.   IVIEW_WINDOW w;
  477.   int i, low, high, cut1, cut2, cut3, z, nz;
  478.   int next, n;
  479.   
  480.   object = xlgaobject();
  481.   vz = getfixnum(xlgafixnum());
  482.   xllastarg();
  483.   
  484.   w = GETIVIEWADDRESS(object);
  485.   if (w == nil) return(NIL);
  486.  
  487.   IViewGetScreenRange(w, vz, &low, &high);
  488.   cut1 = (low + high) / 2 - (high - low) / 8;
  489.   cut2 = (low + high) / 2;
  490.   cut3 = (low + high) / 2 + (high - low) / 8;
  491.   n  = IViewNumPoints(w);
  492.   IViewDepthCuePoints(w, vz, cut1, cut2, cut3, 0, n);
  493.   cut1 = (low + high) / 2 - (high - low) / 8;
  494.   cut3 = (low + high) / 2 + (high - low) / 8;
  495.   n = IViewNumLines(w);
  496.   for (i = 0; i < n; i++) {
  497.     z = IViewLineScreenValue(w, vz, i);
  498.     next = IViewNextLine(w, i);
  499.     nz = (next >= 0)
  500.        ? IViewLineScreenValue(w, vz, next) : z;
  501.     z = (z + nz) / 2;
  502.     if (z < cut1) IViewSetLineWidth(w, i, 1);
  503.     else if (z < cut3) IViewSetLineWidth(w, i, 2);
  504.     else IViewSetLineWidth(w, i, 3);
  505.   }
  506.   return(NIL);
  507. }
  508.